import QQMapWX from "../utils/qqmap-wx-jssdk.js"
class getlocat {
	constructor(city) {
		this.city = ''
	}
	static qqmapsdk = new QQMapWX({
		key: 'BDUBZ-VUWLQ-WDT54-GIK3C-D6S22-KTFHS'
	})

	get value() {
		return this.city
	}
	static getAuthorizeInfo(a = "scope.userLocation") { //1. uniapp弹窗弹出获取授权（地理，个人微信信息等授权信息）弹窗
		let _this = this
		return new Promise((resolve, reject) => {
			uni.authorize({
				scope: a,
				success() { //1.1 允许授权
					resolve(true)
					// _this.getLocationInfo();
				},
				fail() { //1.2 拒绝授权
					console.log("你拒绝了授权，无法获得周边信息")
					uni.showModal({
						title: '微券 需要获取你的地理位置',
						content: '你的位置信息将用于定位附近的门店',
						success: function(res) {
							if (res.cancel) {
								uni.showToast({
									title: '无法获得周边信息',
									icon: 'none',
									duration: 1000
								})
							} else if (res.confirm) {
								uni.openSetting({
									success: function(dataAu) {
										if (dataAu.authSetting[a] == true) {
											uni.showToast({
												title: '授权成功',
												icon: 'success',
												duration: 1000
											})
											resolve(true)
											// _this.getLocationInfo();
										} else {
											uni.showToast({
												title: '授权失败',
												icon: 'none',
												duration: 1000
											})
											reject()
										}
									}
								})
							}
						}
					})
				}
			})
		})
		

	}
	static getLocationInfo() { //2. 获取地理位置
		let _this = this
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'wgs84',
				success(res) {
					console.log("你当前经纬度是：")
					console.log(res)
					let latitude, longitude;
					latitude = res.latitude.toString();
					longitude = res.longitude.toString();
					_this.qqmapsdk.reverseGeocoder({
						location: {
							latitude: latitude,
							longitude: longitude
						},
						success: function(res) {
							let province = res.result.ad_info.province;
							let city = res.result.ad_info.city;
							wx.setStorageSync('province', province);
							wx.setStorageSync('usercity', city);
							resolve(res)
						},
						fail: function(error) {
							reject(error)
						}
					});
				}
			})
		})
	}
	static isGetLocation(a = "scope.userLocation") { // 3. 检查当前是否已经授权访问scope属性，参考下截图
		let _this = this
		if (uni.getStorageSync('usercity')) return false
		// console.log(await _this.getLocationInfo())
		return new Promise(resolve => {
			uni.getSetting({
				success(res) {
					if (!res.authSetting[a]) { //3.1 每次进入程序判断当前是否获得授权，如果没有就去获得授权，如果获得授权，就直接获取当前地理位置
						_this.getAuthorizeInfo()
					} else {
						_this.getLocationInfo().then(res => {
							resolve(res)
						})
					}
				}
			})
		})


	}
}
export default getlocat
